#include "StdAfx.h"
#include "link_list.h"

namespace fly
{
	namespace jld
	{
		namespace ds
		{
			template<typename D>
			link_list<D>::link_list()
				:header_(new node())
				,tail_(header_)
				,size_(0)
			{
			}

			template<typename D>
			link_list<D>::~link_list()
			{
				clear();
			}

			template<typename D>
			void link_list<D>::add(const D& d)
			{
				tail_->next = new node();
				tail_ = tail_->next;

				tail_->d = d;
				++size_;
			}

			template<typename D>
			void link_list<D>::remove(const D& d)
			{
				node* tmp = header_;
				while (tmp->next)
				{
					node* n_tmp = tmp->next;
					if (n_tmp->d == d)
					{
						tmp->next = n_tmp->next;
						delete n_tmp;
						--size_;
						return;
					}
					tmp = n_tmp;
				}
			}

			template<typename D>
			void link_list<D>::pop_front()
			{
				if (0 == size_)
				{
					return;
				}
				node* tmp = header_->next;
				header_->next = tmp->next;
				delete tmp;
				--size_;
			}

			template<typename D>
			void link_list<D>::clear()
			{
				while (0 != size_)
				{
					pop_front();
				}
			}

			template<typename D>
			void link_list<D>::print()
			{
				node* tmp = header_->next;
				while (tmp)
				{
					cout<<tmp->d<<endl;
					tmp = tmp->next;
				}
			}

			template<typename D>
			const size_t& link_list<D>::size()
			{
				return size_;
			}

			template<typename D>
			bool link_list<D>::empty()
			{
				return 0 == size_;
			}
		}
	}
}
